﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SIOsystem.Control;


namespace SIOsystem
{
    public partial class AjustForm : Form
    {
        public string uname = "";
        public long id = -1;
        public string rid = "";
        public AjustForm()
        {
            InitializeComponent();
        }

        private void InQuary_Click(object sender, EventArgs e)
        {
            DataTable dt = ReportClass.getInOrderList(InSD.Value, InED.Value, "");
            if (dt!=null)
            {
                OrderList.DataSource = dt;
                OrderList.Columns["单价"].DefaultCellStyle.Format = "N2";
                OrderList.Columns["金额"].DefaultCellStyle.Format = "N2";
                OrderList.Columns["入库日期"].DefaultCellStyle.Format = "M";
            }
        }

        private void AjustForm_Load(object sender, EventArgs e)
        {
            InSD.Value = InSD.Value.AddMonths(-1);
            OutSD.Value = OutSD.Value.AddMonths(-1);
            DictionaryClass.fillDepartment(ref OutDept);
        }

        private void OutQuary_Click(object sender, EventArgs e)
        {
            DataTable dt = ReportClass.getOutOrderList(OutSD.Value.Date, OutED.Value.AddDays(1).Date, "", "", long.Parse(OutDept.SelectedValue.ToString()));
            if (dt!=null)
            {
                OutOrderList.DataSource = dt;
                OutOrderList.Columns["单价"].DefaultCellStyle.Format = "N2";
                OutOrderList.Columns["金额"].DefaultCellStyle.Format = "N2";
                OutOrderList.Columns["出库日期"].DefaultCellStyle.Format = "M";
            }
        }

        private void OrderList_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (OrderList.SelectedRows.Count == 1)
            {
                InProductName.Text = OrderList.Rows[e.RowIndex].Cells["材料名"].Value.ToString()
                                   + OrderList.Rows[e.RowIndex].Cells["规格"].Value.ToString();
                long ISID = long.Parse(OrderList.Rows[e.RowIndex].Cells["ISID"].Value.ToString());
                Storage.Maximum = decimal.Parse(OrderClass.getCanOut(ISID).ToString());
                Storage.Value = Storage.Maximum;
                NewPrice.Value = decimal.Parse(OrderList.Rows[e.RowIndex].Cells["单价"].Value.ToString());
            }
        }

        private void InAjust_Click(object sender, EventArgs e)
        {
            try
            {
                double oldPrice = double.Parse(OrderList.SelectedRows[0].Cells["单价"].Value.ToString());
                int oldAmt = int.Parse(OrderList.SelectedRows[0].Cells["数量"].Value.ToString());
                if (oldAmt == 0)
                {
                    MessageBox.Show("请填写调整数量！");
                    return;
                }
                if (oldPrice == 0)
                {
                    MessageBox.Show("价格必须大于零！");
                    return;
                }
                long sid = long.Parse(OrderList.SelectedRows[0].Cells["ISID"].Value.ToString());
                OrderClass.updateAmtPrice(sid, oldPrice, Convert.ToInt64(NewPrice.Value)
                                            , oldAmt, Convert.ToInt32(Storage.Value), id,InRemark.Text);
                InQuary_Click(null, null);
                MessageBox.Show("调整成功！");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("调整失败！" + ex.Message);
            }
        }

        private void OutOrderList_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (OutOrderList.SelectedRows.Count == 1)
            {
                OutProductName.Text = OutOrderList.Rows[e.RowIndex].Cells["材料名"].Value.ToString()
                                   + OutOrderList.Rows[e.RowIndex].Cells["规格"].Value.ToString();
                CanBack.Maximum = decimal.Parse(OutOrderList.Rows[e.RowIndex].Cells["数量"].Value.ToString());
                CanBack.Value = CanBack.Maximum;
            }
        }

        private void OutAjust_Click(object sender, EventArgs e)
        {
            try
            {
                double price = double.Parse(OutOrderList.SelectedRows[0].Cells["单价"].Value.ToString());
                int oldAmt = int.Parse(OutOrderList.SelectedRows[0].Cells["数量"].Value.ToString());
                if (oldAmt==0)
                {
                    MessageBox.Show("请填写退仓数量！");
                    return;
                }
                long sid = long.Parse(OutOrderList.SelectedRows[0].Cells["OSID"].Value.ToString());
                OrderClass.updateOutAmt(sid, price, oldAmt, Convert.ToInt32(CanBack.Value), id, OutRemark.Text);
                OutQuary_Click(null, null);
                MessageBox.Show("调整成功！");
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("调整失败！" + ex.Message);
            }
        }
    }
}
